package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.GoodsItemDao;
import com.ktjy.ktmall.entity.GoodsItem;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @auth wzy
 * @date 2024/10/24 12:06
 */
public class GoodsItemDaoImpl extends BaseDao implements GoodsItemDao {
    @Override
    public List<GoodsItem> getGoodsListPage(Integer from, Integer pageSize) {
        List<GoodsItem> list = new ArrayList<>();
        // 查询已上架的商品分页列表
        String sql = "select * from `t_goods_info` where `publish_status` = 1 limit ?,?";
        rs = super.executeQuery(sql, from, pageSize);
        try {
            while (rs.next()) {
                GoodsItem g = new GoodsItem(rs.getInt("id"), rs.getString("goods_name")
                        , rs.getString("spu_code"), rs.getInt("category_id")
                        , rs.getInt("brand_id"), rs.getString("goods_img")
                        , rs.getString("goods_desc"), rs.getBigDecimal("goods_price"),
                        rs.getInt("publish_status"), rs.getInt("order_num")
                        , rs.getString("mainPictures"), rs.getString("detailsPictures")
                        , rs.getString("create_time"), rs.getString("update_time"));
                list.add(g);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    @Override
    public int getCount() {
        int count = 0;
        String sql = "select count(*) from `t_goods_info` where `publish_status` = 1";
        rs = super.executeQuery(sql);

        try {
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return count;
    }

    @Override
    public List<GoodsItem> getGoodsByCategoryId(Integer categoryId) {
        List<GoodsItem> list = new ArrayList<>();
        // 查询已上架的商品分页列表
        String sql = "select * from `t_goods_info` where `publish_status` = 1 AND category_id = ?";
        rs = super.executeQuery(sql, categoryId);
        try {
            while (rs.next()) {
                GoodsItem g = new GoodsItem(rs.getInt("id"), rs.getString("goods_name")
                        , rs.getString("spu_code"), rs.getInt("category_id")
                        , rs.getInt("brand_id"), rs.getString("goods_img")
                        , rs.getString("goods_desc"), rs.getBigDecimal("goods_price")
                        , rs.getInt("publish_status"), rs.getInt("order_num")
                        , rs.getString("mainPictures"), rs.getString("detailsPictures")
                        , rs.getString("create_time"), rs.getString("update_time"));
                list.add(g);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;

    }

    @Override
    public GoodsItem findById(int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        GoodsItem g = null;
        String sql = "select * from `t_goods_info` where id = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            if (!rs.isClosed() && rs.next()) {
                g = new GoodsItem(rs.getInt("id"), rs.getString("goods_name")
                        , rs.getString("spu_code"), rs.getInt("category_id")
                        , rs.getInt("brand_id"), rs.getString("goods_img")
                        , rs.getString("goods_desc"), rs.getBigDecimal("goods_price")
                        , rs.getInt("publish_status"), rs.getInt("order_num")
                        , rs.getString("mainPictures"), rs.getString("detailsPictures")
                        , rs.getString("create_time"), rs.getString("update_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return g;
    }
}
